var $ = getApp(), methods = require("../methods.js");
Page(Object.assign({}, methods, {
	data: {
		tabnum:1,
		ggselected: [],
		ks: '',
		gwcnum: 1,
		showdetail:false,
		buydialogHidden: true,
		team:[],
		list:[],
		product:[],
		rtime:'',
		rtimeformat:''
	},
	_onLoad: function () {
		var that = this;
		var teamid = that.options.teamid
		if (that.options && that.options.scene) {
			var scene = that.options.scene
			var scenes = scene.split('_');
			if(scenes[1]){
				teamid = parseInt(scenes[1]);
			}
		}
		$.get('collage/team', { teamid: teamid}, function (res) {
			that.setData(res);
			var pagecontent = JSON.parse(res.product.detail);
			var _bannerindex = [];
			for(var i in pagecontent){
				_bannerindex[i] = 0;
			}
			that.setData({pagecontent:pagecontent,_bannerindex:_bannerindex});
			that.getrtime();
			setInterval(function(){
				that.getrtime();
			},1000);
		});
	},
	changeshowdetail:function(){
		this.setData({
			showdetail: !this.data.showdetail
		});
	},
	buydialogChange: function (e) {
		this.setData({
			buydialogHidden: !this.data.buydialogHidden
		});
	},
	//选择规格
	ggchange: function (e) {
		var idx = e.currentTarget.dataset.idx;
		var itemk = e.currentTarget.dataset.itemk;
		var ggselected = this.data.ggselected
		ggselected[itemk] = idx
		var ks = ggselected.join(',');
		this.setData({
			ggselected: ggselected,
			ks: ks
		})
	},
	//加
	gwcplus: function (e) {
		var gwcnum = this.data.gwcnum + 1;
		var ggselected = this.data.ks;
		if (gwcnum > this.data.guigelist[ggselected].stock) {
			$.error('库存不足');
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum + 1
		})
	},
	//减
	gwcminus: function (e) {
		var gwcnum = this.data.gwcnum - 1;
		var ggselected = this.data.ks;
		if (gwcnum <= 0) {
			return;
		}
		this.setData({
			gwcnum: this.data.gwcnum - 1
		})
	},
	//输入
	gwcinput: function (e) {
		var ggselected = this.data.ks;
		var gwcnum = parseInt(e.detail.value);
		if (gwcnum < 1) return 1;
		if (gwcnum > this.data.guigelist[ggselected].stock) {
			return this.data.guigelist[ggselected].stock
		}
		this.setData({
			gwcnum: gwcnum
		});
	},
	tobuy: function (e) {
		var type = e.currentTarget.dataset.type
		var that = this;
		var ggselected = that.data.ks;
		var proid = that.data.product.id;
		var ggid = that.data.guigelist[ggselected].id;
		var num = that.data.gwcnum;
		//var prodata = proid + ',' + ggid + ',' + num;
		$.goto('/pages/collage/buy?proid=' + proid + '&num='+num+'&ggid=' + ggid + '&buytype='+type+'&teamid='+that.data.team.id);
	},
	getrtime:function(){
		var rtime = this.data.rtime - 1
		if (rtime<0){
			this.setData({ rtimeformat: '0秒', rtime: rtime })
		}else{
			var hours = Math.floor(rtime / 3600)
			//计算相差分钟数  
			var leave2 = rtime % 3600        //计算小时数后剩余的毫秒数  
			var minutes = Math.floor(leave2 / 60)
			//计算相差秒数  
			var seconds = leave2 % 60      //计算分钟数后剩余的毫秒数
			var rtimeformat = hours + "小时" + minutes + "分" + seconds + "秒"
			this.setData({ rtimeformat: rtimeformat, rtime: rtime})
		}
	},
	//收藏操作
	addfavorite: function () {
		var that = this;
		var proid = that.data.product.id;
		$.post('collage/addfavorite', { proid: proid,type:'collage'}, function (data) {
			if (data.status == 1) {
				that.setData({ isfavorite: !that.data.isfavorite });
			}
			$.success(data.msg);
		}, '加载中');

	},
	tabClick: function (e) {
		this.setData({
			tabnum: e.currentTarget.dataset.num
		})
	},
	shareClick:function(){
		this.setData({sharetypevisible:true})
	},
	handleClickMask:function(){
		this.setData({sharetypevisible:false})
	},
	showPoster:function(){
		var that = this;
		that.setData({showposter:true,sharetypevisible:false});
		$.post('collage/getTeamPoster',{proid:that.data.product.id,teamid:that.data.team.id},function(data){
			if(data.status==0){
				$.alert(data.msg);
			}else{
				that.setData({posterpic:data.poster})
			}
		},'努力生成中')
	},
	posterDialogClose:function(){
		this.setData({showposter:false});
	},
	onShareAppMessage: function (res) {
			var that = this;
			var title = that.data.product.name
			var url = '/pages/collage/team?teamid=' + that.data.team.id + '&scene=' + $.mid;
			console.log(url)
			return {
					title: '就差你了，快来一起拼团~ ' + title,
					path: url,
					imageUrl:that.data.product.pics[0]
			}
	}
}))